#include"carreq.h"

char *http_request(char *base64,long len)
{
	static char req[MAXSIZE];
	bzero(req, MAXSIZE);

	snprintf(req, MAXSIZE, "POST /api/recogliu.do HTTP/1.1\r\n"
			     "Host: anpr.market.alicloudapi.com\r\n"
			     /*"X-Ca-Key: 25539563\r\n"*/
			     "Connection: keep-alive\r\n"	
			     "Content-Length: %ld\r\n"       
			     "Content-Type: application/x-www-form-urlencoded; charset=utf-8\r\n"
			     "Authorization: APPCODE 1033b38b254343108217488f8a25b3fa\r\n\r\n" 
			     "img=%s"    
			     ,len,strtok(base64,"\n"));
	return req;
}

int get_size(char *head)
{
	char *p;
	if((p=strstr(head, "Content-Length:")) != NULL)
	{
		return atoi(p+strlen("Content-Length: "));
	}
}
char *get_cardnumber(char *json)
{
	//获取车牌号码
	cJSON *root = cJSON_Parse(json);
 	if (!root) 
  	{
      	  printf("Error before: [%s]\n",cJSON_GetErrorPtr());
    	}
	cJSON *cardsinfo = cJSON_GetObjectItem(root, "cardsinfo");
	cJSON *number_0 = cJSON_GetArrayItem(cardsinfo,0);
	cJSON *items = cJSON_GetObjectItem(number_0, "items");
	cJSON *items_0 = cJSON_GetArrayItem(items,0);
	char *content = cJSON_GetObjectItem(items_0, "content")->valuestring;	
	
	return content;
}
bool ask4http(int sockfd)
{
	int fd= open("base64.txt",O_RDONLY);
	struct stat finfo;
	stat("base64.txt", &finfo);
	printf("Content-Length:%ld\n",finfo.st_size);
	char *base64= calloc(1,finfo.st_size);
	bzero(base64,finfo.st_size);
	
	int totalm= 0;
	long readm= finfo.st_size;
	while(readm>0)
	{
		int n= read(fd,base64+totalm,readm);
		readm -=n;
		totalm +=n;
	}
	

	
	char *req= http_request(base64,finfo.st_size);
	//printf("%s\n",req);
	
	int len_req = strlen(req);
	int m;
	while(len_req > 0)
	{
		m = write(sockfd, req, strlen(req));
		len_req -= m;
		req += m;
	}
	
	return true;
}
char *get_json(int sockfd)
{
	char response[2046];
	bzero(response, 2046);

	// 4.1 读取响应头部
	for(int i=0; ; i++)
	{
		read(sockfd, response+i, 1);

		if(strstr(response, "\r\n\r\n"))
			break;
	}
	printf("响应头部:\n%s", response);

	// 4.2 读取JSON
	int len_json = get_size(response);
	printf("JSON数据长度:%d\n", len_json);

	char *json = calloc(1, len_json);
	int total = 0;
	while(len_json > 0)
	{
		int n = read(sockfd, json+total, len_json);
		len_json -= n;
		total += n;
	}
	//printf("JSON:\n%s\n", json);
	printf("rm\n");
	system("rm base64.txt");
	return json;
}
/*
int main(int argc,char **argv)
{
	
	struct hostent *he = gethostbyname("anpr.market.alicloudapi.com");
	if(he == NULL)
	{
		printf("获取服务器IP失败: %s\n", strerror(h_errno));
		exit(0);
	}

	char *ip = inet_ntoa(*((struct in_addr *)(he->h_addr_list[0])));
	printf("%s\n", ip);


	// 2，试图连接服务器
	int sockfd = socket(AF_INET, SOCK_STREAM, 0);

	struct sockaddr_in addr;
	socklen_t len = sizeof(addr);
	bzero(&addr, len);

	addr.sin_family = AF_INET;
	addr.sin_addr = *((struct in_addr *)(he->h_addr_list[0]));
	addr.sin_port = htons(80);

	connect(sockfd, (struct sockaddr *)&addr, len);
	printf("连接成功！\n");

	char *command= calloc(1,50);
	snprintf(command,50,"./base64 %s",argv[1]);
	system(command);
	
	/*
	int fd= open("base64.txt",O_RDONLY);
	struct stat finfo;
	stat("base64.txt", &finfo);
	printf("%ld\n",finfo.st_size);
	char *base64= calloc(1,finfo.st_size);
	bzero(base64,finfo.st_size);
	
	int totalm= 0;
	long readm= finfo.st_size;
	while(readm>0)
	{
		int n= read(fd,base64+totalm,readm);
		readm -=n;
		totalm +=n;
	}
	

	
	char *req= http_request(base64,finfo.st_size);
	printf("%s\n",req);
	
	int len_req = strlen(req);
	int m;
	while(len_req > 0)
	{
		m = write(sockfd, req, strlen(req));
		len_req -= m;
		req += m;
	}
	
	if(ask4http(sockfd))
		printf("发送请求报文成功！\n");
	
	
	char response[2046];
	bzero(response, 2046);

	// 4.1 读取响应头部
	for(int i=0; ; i++)
	{
		read(sockfd, response+i, 1);

		if(strstr(response, "\r\n\r\n"))
			break;
	}
	printf("响应头部:\n%s", response);

	// 4.2 读取JSON
	int len_json = get_size(response);
	printf("JSON数据长度:%d\n", len_json);

	char *json = calloc(1, len_json);
	int total = 0;
	while(len_json > 0)
	{
		int n = read(sockfd, json+total, len_json);
		len_json -= n;
		total += n;
	}
	
	char *json= get_json(sockfd);
	printf("JSON:\n%s\n", json);

	char *carnumber= get_cardnumber(json);
	printf("车牌:%s\n",carnumber);


}
*/
